home *** CD-ROM | disk | FTP | other *** search
- /*
- * © 2001 Shawn Platkus.
- *
- * Hack 16 for MacHack 16
- *
- * Main source file for CDaemon
- */
-
- #include "Hack 16 out.h"
-
- // Globals
- bool gbQuit = false;
- const short StackSize = 64;
-
- // Application constructor
- Application::Application()
- {
- Initialize();
- }
-
- void Application::Initialize()
- {
- #ifdef DEVELOPMENT // for debugging
- #if !TARGET_API_MAC_CARBON
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- #endif
- InitCursor();
- #endif
- }
-
- void Application::RunTest()
- {
- #ifdef DEVELOPMENT // for debugging
- char theInputChar;
- #endif
- OSErr status = noErr;
- UInt32 theDeviceNumber;
- SInt16 drvrRefNum = 0;
-
- #ifdef DEVELOPMENT // for debugging
- SIOUXSettings.asktosaveonclose = false; // Metrowerks only
- #endif
-
- #ifdef DEVELOPMENT // for debugging
- // Intro
- printf ("Hello,\nI'm going to try to detect your CD-ROM drive and display some\ninformation about it.\nPress return to continue.\n");
- theInputChar = getchar();
- printf ("OK, here we go...\n\n\n");
- #endif
-
- // Check for ATA Hardware
- // you should do this before calling the ATAManager, since some early ROMS
- // could indicate an ATA manager without the proper HW, thus casing a crash.
- if (ATAHardwarePresent() == false)
- {
- SysBeep(33);
- #ifdef DEVELOPMENT // for debugging
- printf("ATA Hardware is not present on this system\n");
- exit(EXIT_FAILURE);
- #endif
- }
- else
- {
- // Check for ATA Manager
- if (ATAManagerPresent() == false)
- {
- SysBeep(33);
- #ifdef DEVELOPMENT // for debugging
- printf("ATA Manager is not present on this system\n");
- exit(EXIT_FAILURE);
- #endif
- }
- else
- {
- // Display ATA Manager Info
- #ifdef DEVELOPMENT // for debugging
- status = DisplayATAManagerInquiryInfo();
- #endif
- if (status != noErr)
- {
- SysBeep(33);
- #ifdef DEVELOPMENT // for debugging
- printf("Cannot access ATA Manager: %d\n", (int) status);
- exit(EXIT_FAILURE);
- #endif
- }
- else
- {
- // Disable the CD driver
- status = OpenDriver("\p.AppleCD", &drvrRefNum);
- if (status == noErr)
- {
- #ifdef DEVELOPMENT // for debugging
- printf("Found .AppleCD driver, preparing to disable it\n");
- status = DisableCDDriver(drvrRefNum);
- #endif
- }
-
- // Display ATA Device Info
- theDeviceNumber = ScanATABusses();
- if (theDeviceNumber == -1)
- {
- #ifdef DEVELOPMENT // for debugging
- printf("No CD ROM Drive Found or ATA Manager Error: %d\n", (int) theDeviceNumber);
- //exit(EXIT_FAILURE);
- #endif
- }
- else
- {
- // Try to eject the CD Drive.
- #ifdef DEVELOPMENT // for debugging
- printf ("OK, I'm going to try to eject your CD ROM Drive at device #%i\n", theDeviceNumber);
- printf ("Press Return To Continue...\n");
- theInputChar = getchar();
- #endif
- status = SetupATAPI(theDeviceNumber);
- if (status == noErr)
- {
- status = xJect(true);
- }
- #ifdef DEVELOPMENT // for debugging
- printf (" The result that I got was %i\n", status);
- printf ("-=> ");
- theInputChar = getchar();
-
- // Try to INject the CD Drive.
- printf ("OK, I'm going to try to INject your CD ROM Drive at device #%i\n", theDeviceNumber);
- printf ("Press Return To Continue...\n");
- theInputChar = getchar();
- #endif
- status = xJect(false);
-
- #ifdef DEVELOPMENT // for debugging
- printf ("Did the CD-Drive INject or close? (type yes or no and then return to continue)\n");
- printf (" The result that I got was %i\n", status);
- printf ("-=> ");
- theInputChar = getchar();
- #endif
- }
-
- // Renable the CD driver
- if(drvrRefNum != 0)
- {
- #ifdef DEVELOPMENT // for debugging
- printf("Re-enabling the .AppleCD driver \n");\
- printf("Execution complete... program ended\n\n");
- #endif
- status = EnableCDDriver(drvrRefNum);
- }
- }
- }
- }
- }
-
-
- void Application::IncreaseFBAStack(Size extraBytes)
- {
- THz myZone;
- SysEnvRec sys;
-
- SysEnvirons(1, &sys);
-
- if (sys.systemVersion < 0x0755)
- {
- // Check that we aren't running with a corrupt heap. If we are,
- // fix the problem. This was a bug with FBA's before System 7.5.5.
- myZone = GetZone();
- if (myZone->bkLim != LMGetHeapEnd())
- LMSetHeapEnd(myZone->bkLim);
- }
- // Increase the stack size by lowering the heap limit.
- SetApplLimit((Ptr) ((Size) GetApplLimit() - extraBytes));
- }
-
-
- void Application::CheckEventQueue()
- {
- EventRecord anEvent;
- OSErr err;
-
- WaitNextEvent(highLevelEventMask, &anEvent, 120, NULL);
-
- switch (anEvent.what)
- {
- case kHighLevelEvent :
- err = DoAppleEvent(&anEvent);
- break;
- }
-
- xJect(false);
- }
-
-
- void Application::Run()
- {
-
- OSErr status;
- IncreaseFBAStack(StackSize * 1024);
-
- MaxApplZone();
-
- status = InitAppleEvents();
- if (status != noErr)
- {
- SysBeep(33);
- #ifdef DEVELOPMENT // for debugging
- printf("This program failed to initialize Apple events\n\n");
- #endif
- }
- else
- {
- RunTest();
-
- while ( ! gbQuit )
- {
- CheckEventQueue();
-
- #ifdef DEVELOPMENT // for debugging
- gbQuit = true;
- #endif
- }
- }
- }
-
-
- void main(void)
- {
- Application theApplication;
-
- theApplication.Run();
- }
-